<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%--
  Created by IntelliJ IDEA.
  User: 董灌磊
  Date: 2024/4/12
  Time: 10:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/webjars/layui/2.6.8/css/layui.css">
    <script src="/webjars/layui/2.6.8/layui.js"></script>
</head>
<body>
<div class="layui-form" id="searchForm" lay-filter="searchForm">
    <div class="layui-inline">
        <label class="layui-form-label">规则名称:</label>
        <div class="layui-input-inline">
            <input type="text" name="rulename" placeholder="输入规则名称" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <button class="layui-btn" onclick="search()" >查询</button>
    </div>
</div>
<table id="parkruleTable" lay-filter="parkruleTable"  class="layui-table layui-table-hover"></table>
<%--添加页面--%>
<div id="add" style="display:none;padding:20px">
    <form class="layui-form" id="addForm" lay-filter="addForm">
        <div class="layui-form-item">
            <label class="layui-form-label">规则名称:</label>
            <div class="layui-input-block">
                <input type="text" name="rulename" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">免费分钟:</label>
            <div class="layui-input-block">
                <input type="text" name="freetime" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">小时收费:</label>
            <div class="layui-input-block">
                <input type="text" name="hoursemoney" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">收费金额:</label>
            <div class="layui-input-block">
                <input type="text" name="money" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">24小时收费限制:</label>
            <div class="layui-input-block">
                <input type="text" name="daymoney" required  class="layui-input">
            </div>
        </div>
    </form>
</div>
<%--修改页面--%>
<div id="update" style="display:none;padding:20px">
    <form class="layui-form" id="updateForm" lay-filter="updateForm">
        <div class="layui-form-item">
            <label class="layui-form-label">规则名称:</label>
            <div class="layui-input-block">
                <input type="hidden" name="id">
                <input type="text" name="rulename" required  class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">免费分钟:</label>
            <div class="layui-input-block">
                <input type="text" name="freetime" required  class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">小时收费:</label>
            <div class="layui-input-block">
                <input type="text" name="hoursemoney" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">收费金额:</label>
            <div class="layui-input-block">
                <input type="text" name="money" required  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">24小时收费限制:</label>
            <div class="layui-input-block">
                <input type="text" name="daymoney" required  class="layui-input">
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="update">编辑</a>
    </div>
</script>
<%--头部工具栏，添加--%>
<script type="text/html" id="headTool">
    <button class="layui-btn" onclick="add()">添加规则</button>
</script>
<script>
    var $=layui.$
    layui.use(function (){
        let table=layui.table;
        table.render({
            id:"one",
            elem:"#parkruleTable",
            url:"/park/parkrule/show",
            toolbar: "#headTool",
            page:true,
            cols:[[
                {field:'number',align: 'center',type:'numbers',title:"编号"},
                {field:'id',align: 'center',hide:true,title:"序号"},
                {field:'rulename',align: 'center',title:"规则名称"},
                {field:'freetime',align: 'center',title:"免费分钟"},
                {field:'hoursemoney',align: 'center',title:"小时收费"},
                {field:'money',align: 'center',title:"收费金额"},
                {field:'daymoney',align: 'center',title:"24小时收费限制"},
                {align: 'center',title:"操作",toolbar:("#barDemo")},
            ]]
        })
        //监听行工具栏事件
        table.on("tool(parkruleTable)",function (obj){
           if(obj.event == "update"){
                updates(obj);
            }
        })
    })
    //点击去查询
    function search(){
        //获取表单的值,表单序列化
        var where = layui.form.val('searchForm');
        console.log(where)
        //条件查询，重载表格因为键和值都是where，所以可以值保留键
        layui.table.reload('one',{where});
    }
    //点击添加
    function add(){
        $("#addForm")[0].reset();

        layui.layer.open({
            type : 1,
            title : "提示",
            area:["500px","400px"],
            content: $("#add"),
            icon : 3,
            btn:["确定","取消"],
            yes(){
                //通过表单序列化，获取输入的内容
                let addForm = layui.form.val("addForm");
                console.log(addForm);
                if(addForm.rulename==null||addForm.rulename==''){
                    layui.layer.msg("请填写完整")
                    return;
                }else if(addForm.freetime==null||addForm.freetime==''){
                    layui.layer.msg("请填写完整")
                    return;
                }   else if(addForm.hoursemoney==null||addForm.hoursemoney==''){
                    layui.layer.msg("请填写完整")
                    return;
                }   else if(addForm.money==null||addForm.money==''){
                    layui.layer.msg("请填写完整")
                    return;
                }   else if(addForm.daymoney==null||addForm.daymoney==''){
                    layui.layer.msg("请填写完整")
                    return;
                }else {
                    //发送ajax请求
                    $.post("/park/parkrule/add",addForm)
                        .then(({msg})=>{
                            //关闭弹出层
                            layui.layer.closeAll();
                            //弹出消息提示框
                            layui.layer.msg(msg);
                            //刷新表格
                            layui.table.reload('one');
                        });
                }
            }
        });
    }
    function  updates(obj){
        console.log(obj.data);
        //先赋值
        layui.form.val('updateForm', obj.data);
        layui.layer.open({
            type : 1,
            title : "修改页面",
            area:["500px","400px"],
            content: $("#update"),
            icon : 3,
            btn:["确定","取消"],
            yes(){
                //通过表单序列化，获取输入的内容
                let updateForm = layui.form.val("updateForm");
                console.log(updateForm);
                //发送ajax请求
                $.post("/park/parkrule/update",updateForm)
                    .then(({msg})=>{
                        //关闭弹出层
                        layui.layer.close(layui.layer.index);
                        //弹出消息提示框
                        layui.layer.msg(msg);
                        //刷新表格
                        layui.table.reload('one');
                    });
            }
        });
    }
</script>
</body>
</html>
